package com.zust.zst.controller.chooselesson;

import com.zust.zst.chooselesson.service.CultivationSystemService;
import com.zust.zst.common.dto.chooselesson.AddCultivationPlanDTO;
import com.zust.zst.common.dto.chooselesson.ColGraMajDTO;
import com.zust.zst.common.vo.ResultVO;
import com.zust.zst.common.vo.chooselesson.CultivationPlanVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.zust.zst.common.enums.OperationStatus.INSERT_FAILED;

/**
 * 选课管理：培养计划接口
 *
 * @author Gavin Zhang
 * @date 2022/12/7 16:15
 */

@RestController
@Api(tags = "选课管理：培养计划接口")
@Slf4j
@RequestMapping("/chooselesson/cultivationSystem")
@Validated
public class CultivationSystemController {

    @Resource(name = "cultivationSystemServiceImpl")
    private CultivationSystemService cultivationSystemService;

    @ApiOperation(value = "获取对应二级学院的全部历史学年")
    @GetMapping("/getAllGrade/{college}")
    public ResultVO<List<Integer>> getAllGradeFromCollege(@PathVariable String college) {

        List<Integer> list = cultivationSystemService.getAllGradeFromCollege(college);
        if (!CollectionUtils.isEmpty(list)) {
            log.info("通过{}查询到了其历史学年：{}", college, list);
            return ResultVO.success(list);
        }
        log.warn("未查到对应学年结果，请检查输入是否有误");
        return ResultVO.error("未查到对应学年结果，请检查输入是否有误");
    }

    @ApiOperation(value = "获取对应二级学院及特定年级的所有专业")
    @GetMapping("/getMajorByCollegeAndGrade/{college}/{grade}")
    public ResultVO<List<String>> getMajorByCollegeAndGrade(@PathVariable String college, @PathVariable Integer grade) {

        List<String> list = cultivationSystemService.getMajorByCollegeAndGrade(college, grade);
        if (!CollectionUtils.isEmpty(list)) {
            log.info("通过学院：{}，年级：{}，查询到了其专业：{}", college, grade, list);
            return ResultVO.success(list);
        }
        log.info("未查到对应专业结果，请检查输入是否有误");
        return ResultVO.error("未查到对应专业结果，请检查输入是否有误");
    }

    @ApiOperation(value = "获取某专业所有培养计划课程的总条数")
    @PostMapping("/getTotalByCultivationPlan")
    public ResultVO<Integer> getTotalByCultivationPlan(@RequestBody @Valid ColGraMajDTO colGraMajDTO) {

        Integer count = cultivationSystemService.getTotalByCultivationPlan(colGraMajDTO);
        log.info("通过学院：{}，年级：{}，专业：{}，查询到培养计划条数：{}", colGraMajDTO.getCollege(), colGraMajDTO.getGrade(), colGraMajDTO.getMajor(), count);
        return ResultVO.success("该专业所有培养计划课程的条数条数", count);
    }

    @ApiOperation(value = "获取某专业所有培养计划课程")
    @PostMapping("/getCultivationByCollegeAndGradeAndMajor")
    public ResultVO<List<CultivationPlanVO>> getCultivationByCollegeAndGradeAndMajor(@RequestBody @Valid ColGraMajDTO colGraMajDTO) {
        List<CultivationPlanVO> list = cultivationSystemService.getCultivationByCollegeAndGradeAndMajor(colGraMajDTO);
        if (!CollectionUtils.isEmpty(list)) {
            log.info("通过学院：{}，年级：{}，专业：{}，查询到培养计划：{}", colGraMajDTO.getCollege(), colGraMajDTO.getGrade(), colGraMajDTO.getMajor(), list);
            return ResultVO.success("培养计划", list);
        }
        log.info("未能查询到培养计划...学院：{}，年级：{}，专业：{}", colGraMajDTO.getCollege(), colGraMajDTO.getGrade(), colGraMajDTO.getMajor());
        return ResultVO.error("未能获取到对应的培养计划");
    }

    @ApiOperation(value = "添加培养计划课程")
    @PostMapping("/addCultivationPlanCourse")
    public ResultVO<String> addCultivationPlanCourse(@RequestBody @Valid AddCultivationPlanDTO dto) {
        if (cultivationSystemService.addCultivationPlanCourse(dto) == INSERT_FAILED.getCode()) {
            log.warn("插入失败：{}", dto);
            return ResultVO.error(INSERT_FAILED.getDescription());
        }
        log.info("插入成功：{}", dto);
        return ResultVO.success("插入成功", "");
    }
}
